local CommonOHOH = _G.CommonOHOH;
local ServerOHOH = _G.ServerOHOH;
local Rank_Cfg = ServerOHOH.config.Rank
local OHOHEnum = ServerOHOH.load.OHOHEnum();

---PVP 挑战数据库
local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseComponent());
local DataBaseManager = ServerOHOH.load.DataBaseManager();

local gAllRankId={}

function GetRankDataBaseNameByPoint(point)
    for k,v in ipairs(Rank_Cfg) do
        if point<=v.point then
            return GetRankDataBaseNameByLevel(v.rankLevel)
        end
    end
end

function GetRankDataBaseNameByLevel(randkLevel)
       return "pvp_rank_"..randkLevel
end

for k, v in pairs(Rank_Cfg) do
    local randId=GetRankDataBaseNameByLevel(v.rankLevel)
    if not gAllRankId[randId] then
        DataBaseManager:CreateSortDataBase(randId)
        gAllRankId[randId]=true

    end
end;



function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);	

end;

function M.dtor(self)
	M.super.dtor(self);
end;

local pvpList=require(script.pvp)
local gRankCount=50 --最大查询个数


function M.PrintUserData(self, userId)
    print("--------正在打印玩家数据------1----------",userId)

    self:AddProcess(function()

        local success, userData = DataBaseManager:Load(OHOHEnum.data_base.player_data, userId);

        local tryCount = 0;

        --不成功获取数据，重复5次，否则踢出
		while not success do
			tryCount = tryCount + 1;
			if tryCount > 2 then --获取用户数据失败
                    warn("获取车数据失败")
                return 
			end;
			local s, d = DataBaseManager:Load(OHOHEnum.data_base.player_data, userId);
			success = s;
			userData = d;
			wait(5);		
		end;
        print("--------正在打印玩家数据----------------")
        CommonOHOH:PrintTable(userData)
    end)

end

--更新玩家PVP时所在的对象池
function M.UpdateRank(self, point,userId)
    local baseName=GetRankDataBaseNameByPoint(point)
    assert(baseName,"baseName 是空!")

    for v,j in pairs(gAllRankId) do
        if v==baseName then
            DataBaseManager:Save(baseName, userId, function()
                return point
            end);
            print("保存数据数据-->:",baseName,userId,point)
        else
            print("清除数据-->:",v,point,userId)
            DataBaseManager:Clear(v, userId, function()
                    return 0
            end);
        end
    end

end;



-----------------------对外公开-----------------------------------------------
--获取PVP对战数据库（是一个迭代器）参数：传入玩家积分
function M.GetPvpDataStoreByRankId(self, point)
    local baseName=GetRankDataBaseNameByPoint(point)
    local s=string.format("baseName 是空! %s_%s",baseName,point)
    assert(baseName,s)
    local data= DataBaseManager:GetSortedList(baseName, false, gRankCount);

    print("打印获取的匹配数据--->",baseName)

    CommonOHOH:PrintTable(data)

    return  pvpList.new(data)
end;



--更新玩家PVP时所在的对象池
function M.UpdatePlayerPvpPool(self,userId)

    self:AddProcess(function()
        local success, userData = DataBaseManager:Load(OHOHEnum.data_base.player_data, userId);
        local tryCount = 0;
        --不成功获取数据，重复5次，否则踢出
		while not success do
			tryCount = tryCount + 1;
			if tryCount > 2 then --获取用户数据失败
                    warn("获取车数据失败")
                return 
			end;
			local s, d = DataBaseManager:Load(OHOHEnum.data_base.player_data, userId);
			success = s;
			userData = d;
			wait(5);		
		end;

        self:UpdateRank(userData.base.elo,userId)
    end)
end



--获取玩家的车数据
function M.GetPlayerCarInfo(self,userId,callBack)

    self:AddProcess(function()

        local success, userData = DataBaseManager:Load(OHOHEnum.data_base.player_data, userId);

        local tryCount = 0;

        --不成功获取数据，重复5次，否则踢出
		while not success do
			tryCount = tryCount + 1;
			if tryCount > 2 then --获取用户数据失败
                    warn("获取车数据失败")
                return 
			end;
			local s, d = DataBaseManager:Load(OHOHEnum.data_base.player_data, userId);
			success = s;
			userData = d;
			wait(5);		
		end;

        print("打印获取的车数据--->")

        CommonOHOH:PrintTable(userData.car)

        callBack(userData.car)
    end)
end

-- 增加积分（传负数，则是减少积分）
function M.AddPoint(self, userId,count)

    self:AddProcess(function()
        local success, userData = DataBaseManager:Load(OHOHEnum.data_base.player_data, userId);
        if not success then
            warn("添加积分失败---->")
            return 
        end
        userData.base.elo= userData.base.elo+count
        DataBaseManager:Save(OHOHEnum.data_base.player_data, userId, function()
            return userData
        end);

    end)

end

--添加PVP数据,参数，用户名，pvp比赛数据，point 增加减少积分
function M.AddPvpData(self, userId,data,point)
    local success, userData = DataBaseManager:Load(OHOHEnum.data_base.player_data, userId);
    if not success then
        warn("存储PVP数据失败---->")
        return 
    end
    table.insert(userData.PVPData,data)
    if point then
        userData.base.elo= userData.base.elo+point
    end
    DataBaseManager:Save(OHOHEnum.data_base.player_data, userId, function()
        return userData
    end);
end



return M